package com.atguigu.tingshu.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.util.MongoUtil;
import com.atguigu.tingshu.model.user.UserListenProcess;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import io.swagger.v3.core.util.Json;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings({"all"})
public class UserListenProcessServiceImpl implements UserListenProcessService {

	@Autowired
	private MongoTemplate mongoTemplate;
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Override
	public Map<String, Object> getLatelyTrack(Long userId) {
		String collectionName = MongoUtil.getCollectionName(MongoUtil.MongoCollectionEnum.USER_LISTEN_PROCESS, userId);
		Query query = Query.query(Criteria.where("userId").is(userId));
		query.with(Sort.by(Sort.Direction.DESC, "updateTime"));
		UserListenProcess userListenProcess = mongoTemplate.findOne(query, UserListenProcess.class, collectionName);
		if (null!=userListenProcess){
			HashMap<String, Object> hashMap = new HashMap<>();
			hashMap.put("trackId",userListenProcess.getTrackId());
			hashMap.put("albumId",userListenProcess.getAlbumId());
			return hashMap;
		}
		return null;
	}

	@Override
	public void updateListenProcess(UserListenProcessVo userListenProcessVo, Long userId) {
		String collectionName = MongoUtil.getCollectionName(MongoUtil.MongoCollectionEnum.USER_LISTEN_PROCESS, userId);
		Query query = Query.query(Criteria.where("trackId").is(userListenProcessVo.getTrackId()));
		//根据id查询当前存储对象信息
		UserListenProcess userListenProcess = mongoTemplate.findOne(query, UserListenProcess.class, collectionName);
		if (null!=userListenProcess){
			userListenProcess.setBreakSecond(userListenProcessVo.getBreakSecond());
			userListenProcess.setUpdateTime(new Date());
		}else {
			userListenProcess=new UserListenProcess();
			userListenProcess.setUserId(userId);
			userListenProcess.setAlbumId(userListenProcessVo.getAlbumId());
			userListenProcess.setTrackId(userListenProcessVo.getTrackId());
			userListenProcess.setBreakSecond(userListenProcessVo.getBreakSecond());
			userListenProcess.setIsShow(1);
			userListenProcess.setCreateTime(new Date());
			userListenProcess.setUpdateTime(new Date());
		}
		mongoTemplate.save(userListenProcess,collectionName);
		String key=userId+":"+userListenProcess.getTrackId();
		Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, "1", 24, TimeUnit.HOURS);
		if (ifAbsent){
			//发送异步消息专辑+声音播放量+1
			TrackStatMqVo trackStatMqVo = new TrackStatMqVo();
			trackStatMqVo.setBusinessNo(UUID.randomUUID().toString().replaceAll("-",""));
			trackStatMqVo.setAlbumId(userListenProcess.getAlbumId());
			trackStatMqVo.setTrackId(userListenProcess.getTrackId());
			trackStatMqVo.setStatType(SystemConstant.ALBUM_STAT_PLAY);
			trackStatMqVo.setCount(1);
			rabbitTemplate.convertAndSend(MqConst.EXCHANGE_TRACK,MqConst.ROUTING_TRACK_STAT_UPDATE, JSON.toJSONString(trackStatMqVo));
		}
	}

	@Override
	public BigDecimal getTrackBreakSecond(Long trackId, Long userId) {
		String collectionName = MongoUtil.getCollectionName(MongoUtil.MongoCollectionEnum.USER_LISTEN_PROCESS, userId);
		Query query = Query.query(Criteria.where("trackId").is(trackId));
		UserListenProcess userListenProcess = mongoTemplate.findOne(query, UserListenProcess.class, collectionName);
		if (null!=userListenProcess) {
			return userListenProcess.getBreakSecond();
		}
		return new BigDecimal("0.00");
	}
}
